package com.ruoyi.portal.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.portal.domain.PorNewsType;
import com.ruoyi.portal.service.IPorNewsTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 新闻类型Controller
 *
 * @author cjie
 * @date 2021-05-20
 */
@Api(tags = " 新闻类型")
@RestController
@RequestMapping("/newsType")
public class PorNewsTypeController extends BaseController {
    @Autowired
    private IPorNewsTypeService porNewsTypeService;

    /**
     * 查询新闻类型列表
     */
    @ApiOperation(value = "查询新闻类型列表", notes = "查询新闻类型列表")
    @GetMapping("/list")
    public TableDataInfo list(PorNewsType porNewsType) {
        startPage();
        List<PorNewsType> list = porNewsTypeService.selectPorNewsTypeList(porNewsType);
        return getDataTable(list);
    }

    /**
     * 导出新闻类型列表
     */
    @ApiOperation(value = "导出新闻类型列表", notes = "导出新闻类型列表")
    @PreAuthorize(hasPermi = "portal:type:export")
    @Log(title = "新闻类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PorNewsType porNewsType) throws IOException {
        List<PorNewsType> list = porNewsTypeService.selectPorNewsTypeList(porNewsType);
        ExcelUtil<PorNewsType> util = new ExcelUtil<PorNewsType>(PorNewsType.class);
        util.exportExcel(response, list, "新闻类型数据");
    }

    /**
     * 获取新闻类型详细信息
     */
    @ApiOperation(value = "获取新闻类型详细信息", notes = "获取新闻类型详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(porNewsTypeService.selectPorNewsTypeById(id));
    }

    /**
     * 新增新闻类型
     */
    @ApiOperation(value = "新增新闻类型", notes = "新增新闻类型")
    @PreAuthorize(hasPermi = "portal:type:add")
    @Log(title = "新闻类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PorNewsType porNewsType) {
        return toAjax(porNewsTypeService.insertPorNewsType(porNewsType));
    }

    /**
     * 修改新闻类型
     */
    @ApiOperation(value = "修改新闻类型", notes = "修改新闻类型")
    @PreAuthorize(hasPermi = "portal:type:edit")
    @Log(title = "新闻类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PorNewsType porNewsType) {
        return toAjax(porNewsTypeService.updatePorNewsType(porNewsType));
    }

    /**
     * 删除新闻类型
     */
    @ApiOperation(value = "删除新闻类型", notes = "删除新闻类型")
    @ApiImplicitParam(name = "ids", value = "新闻类型ID数组", required = true, paramType = "path", allowMultiple = true, dataType = "String")
    @PreAuthorize(hasPermi = "portal:type:remove")
    @Log(title = "新闻类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(porNewsTypeService.deletePorNewsTypeByIds(ids));
    }
}
